<?php
class Songs_model extends Model {
	function Songs_model()
    {
		parent::Model();
	}
	function upload()
	{
		
		$this->db->insert('songs',array('name' => $_POST['name'], 'time' => $_POST['time']));
		$id = $this->db->insert_id();
			
 	    $dir =  './assets/mp3';   
	    $config['upload_path'] = $dir;
		$config['allowed_types'] = 'mp3';
		$config['max_size']	= '500000';
    	
		
		$this->load->library('upload', $config);
	
		if (!$this->upload->do_upload('song'))
		{
			$error = array('error' => $this->upload->display_errors());
			print_r($error);
			//$this->load->view('admin_images', $error);
		}	
		else
		{
		   $data = array('upload_data' => $this->upload->data());
	       $filepath = $data['upload_data']['file_name'];
	       rename($dir.'/'.$filepath , $dir.'/song'.$id.'.mp3');	
		}
	}
	function getsongs($limit)
	{
		if($limit!=0)
		{
		$this->db->limit($limit);
		}
		$this->db->order_by('id','desc');
		$query = $this->db->get('songs');
		return $query;
		
	}
	function deletesong($id)
	{
		$this->db->where('id',$id);
		$this->db->delete('songs');
		unlink('./assets/mp3/song'.$id.'.mp3');
	}
}
?>